package com.chezhujie.www.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.chezhujie.www.constant.AesConstant;
import com.chezhujie.www.domain.*;
import com.chezhujie.www.domain.vo.CzjawardonVO;
import com.chezhujie.www.domain.vo.CzjawardonVOB;
import com.chezhujie.www.domain.vo.CzjawarduseALLMessage;
import com.chezhujie.www.domain.vo.CzjawarduseVO;
import com.chezhujie.www.enums.OrderStatus;
import com.chezhujie.www.service.*;
import com.xy.core.page.Pagination;
import com.xy.core.utils.Award;
import com.xy.core.utils.Base64;
import com.xy.core.utils.OrderNumUtils;
import com.xy.user.base.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;




@Controller
@RequestMapping("/awardapi")
public class  CzjawardapiController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(CzjcalController.class);
		
	public CzjawardapiController() {
	}
	
	/**
	 * czj_roulette  二维码页面
	 *
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("servicenumAndTel.do")
	public String qrtwoCode(HttpServletRequest request,
							@RequestParam(required=true) String servicenum,
							@RequestParam(required=true) String tel,
							ModelMap model) {
		String erweimamessage = "";				
			String str=servicenum+","+AesConstant.SERVICENUMANDTEL+","+tel;
			erweimamessage =  Base64.getBase64(str);
		request.setAttribute("erweimamessage",erweimamessage);	
		return "qrcode/servicenumAndTel";
	}	
	
   /**
    * 
    * @author 
    * @date 2017年7月5日 下午6:12:57
    * @param awarduseId
    * @param num
    * @param request
    * @param response
    */
	@RequestMapping("generateCode.do")
	public void generateCode(@RequestParam(required=true)String awarduseId,
			HttpServletRequest request,HttpServletResponse response){
	
		try {	
			String str=awarduseId+","+AesConstant.JIANGPIN;
            
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("awarduseId", awarduseId);
			CzjawarduseALLMessage czjawarduse = czjawarduseService.getCzjawarduseALLMessage(param);			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("code", Base64.getBase64(str));
			map.put("actPath",czjawarduse.getActPath());
			map.put("backgroundPath", czjawarduse.getBackgroundPath());
			jsonResult(1, "", "生成奖品二维码密匙成功", map, response);									
		} catch (Exception e) {
			e.printStackTrace();
			log.error("err",e);
			jsonResult(0, "201", "生成奖品二维码密匙错误", "", response);
		}
	}	
	
    /***
     * 
     * @author 
     * @date 2017年5月27日 下午2:11:56
     * @param request
     * @param response
     * @param model
     */
	@RequestMapping("awardon.do")
	public synchronized void  awardon( HttpServletRequest request,
			@RequestParam(required=true) String rouletteId,
			@RequestParam(required=true) String userInfoId,
			HttpServletResponse response, ModelMap model) {
		try {
			//查询用户存在才能抽奖
			Map<String, Object> paramUserId = new HashMap<String, Object>();
			paramUserId.put("id", userInfoId);
			Czjuserinfo czjuserinfo = czjuserinfoService.getCzjuserinfo(paramUserId);
			if(czjuserinfo == null){
				jsonResult(0,"305","不存在的用户不给与抽奖","",response);
				return;
			}		
			//查询活动是否开始与结束
			Map<String, Object> roulettepParam = new HashMap<String, Object>();
			roulettepParam.put("rouletteId", rouletteId);
			Czjroulette czjroulette = czjrouletteService.getCzjroulette(roulettepParam);
			Date date = new Date();
			if(czjroulette == null){
				jsonResult(0,"301","不存在的转盘","",response);
				return;
			}else{
			   	if(date.getTime()<czjroulette.getStarttime().getTime()){
			   		jsonResult(0,"302","活动还没开始","",response);
			   		return;
			   	}
				
			   	if(date.getTime()>czjroulette.getEndtime().getTime()){
			   		jsonResult(0,"303","活动已经结束","",response);
			   		return;
			   	}
			}
			
			
			//限制每个人只能抽一次轮盘
			Czjawarduse czjawarduse1 = queryAwar(rouletteId,userInfoId);
			if(czjawarduse1 !=null){
				Map<String, Object> map1 = new HashMap<String, Object>();
				  map1.put("ext1", czjawarduse1.getExt1());
				  map1.put("ext2", czjawarduse1.getExt2());
				  map1.put("name",czjawarduse1.getAwardId());
				  map1.put("awardId",czjawarduse1.getAwarduseId());
				  map1.put("userState",1);
			      map1.put("received",11);
			        Map<String, Object> map = new HashMap<String, Object>();
			        map.put("status", 1);
			        map.put("code", "");
			        map.put("msg", "一个轮盘只能抽一次奖");
			        map.put("result", map1);
			        writeJson(response, JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue));				  
				 return;
			}
			//查出对应轮盘所有的奖品和奖品库限制的数量
			Map<String, Object> param = new HashMap<String, Object>();	
			param.put("rouletteId",rouletteId);
			List<CzjawardonVOB> list = czjawardapiService.getawardonListALLVOB(param);
              
			//奖品数量多少
			int listTrue = list.size();
			if(listTrue ==0){				
				jsonResult(0,"123","没有分配奖品信息","",response);	
				return;
			}
			//变化之前的多少数量
            int after = list.size();
			
			
			for(int i = list.size() -1 ; i >= 0; i--){
				CzjawardonVOB czjawardonVOB = list.get(i);
				
				if(czjawardonVOB.getFenpeicount()==null){
					czjawardonVOB.setFenpeicount(0);
				}
				if(czjawardonVOB.getChouqunum()==null){
					czjawardonVOB.setChouqunum(0);
				}				
                int fenpei =  czjawardonVOB.getFenpeicount();
                int chouqu =  czjawardonVOB.getChouqunum();
                if(chouqu >= fenpei || czjawardonVOB.getProbability()==0 ){                	     
                	list.remove(i);                												  	
			    }
			}
			//变化后的代码
			after = list.size();
			
			if(listTrue > after &&  after ==0){				
				jsonResult(0,"110","奖品已经抽取完了","",response);	
				return;
			}
			
			List<Award> awards = new ArrayList<Award>();
			//放到抽奖工具类
			for(CzjawardonVOB czjawardonVOB : list){
				Award award = new Award();
				award.setId(czjawardonVOB.getAwardstockId());
				award.setProbability(czjawardonVOB.getProbability());
				award.setName(czjawardonVOB.getThingName());
				award.setExt1(czjawardonVOB.getExt1());
				award.setExt2(czjawardonVOB.getExt2());
				award.setExt3(czjawardonVOB.getShangpinId());
				award.setFestivalActId(czjawardonVOB.getFestivalActId());
				award.setBusinessId(czjawardonVOB.getBusinessId());
				award.setDiscountPrice(czjawardonVOB.getDiscountPrice());
				award.setPrice(czjawardonVOB.getPrice());
				award.setDiscountType(czjawardonVOB.getDiscountType());
				award.setAwardonId(czjawardonVOB.getAwardonId());
				award.setSmallprice(czjawardonVOB.getSmallprice());
				awards.add(award);
			}
			//开始抽奖
			 Map<String,Object> arardMap = new HashMap<String, Object>();
			 if(awards.size()>0){
			 Award award = Award.lottery(awards);	
			 arardMap.put("awardId",award.getId());
			 arardMap.put("name",award.getName());
			 arardMap.put("ext1",award.getExt1());
			 //插入奖品表
			 Czjawarduse czjawarduse = new Czjawarduse();
			 czjawarduse.setAwardId(award.getId());
			 czjawarduse.setUserInfoId(userInfoId);
			 czjawarduse.setRouletteId(rouletteId);
			 czjawarduse.setAwardType(1);
			 czjawarduse.setExt1(award.getExt1());
			 czjawarduse.setExt2(award.getExt2());
			 czjawarduse.setExt3(award.getExt3());
			 czjawarduse.setFestivalActId(award.getFestivalActId());
			 czjawarduse.setBusinessId(award.getBusinessId());
			 czjawarduse.setDiscountType(award.getDiscountType());
			 czjawarduse.setDiscountPrice(award.getDiscountPrice());
			 czjawarduse.setPrice(award.getPrice());
			 czjawarduse.setSmallprice(award.getSmallprice());
			 //奖品库信息插入来
			 
			 Map<String, Object> getCzjawardonParm = new HashMap<String, Object>();
			 getCzjawardonParm.put("awardonId", award.getAwardonId());
			 Czjawardon czjawardon = czjawardonService.getCzjawardon(getCzjawardonParm);
			 if(czjawardon!=null){
				 czjawarduse.setProbability(czjawardon.getProbability());
				 czjawarduse.setAwardcount(czjawardon.getCount());
				 czjawarduse.setAwardOrder(Integer.valueOf(czjawardon.getExt1()));
				 czjawarduse.setIstrue(1);
				 czjawarduse.setChouqunum(Integer.valueOf(czjawardon.getExt3()));
				 czjawarduse.setActPath(czjawardon.getPrizePath());
				 czjawarduse.setBackgroundPath(czjawardon.getBackgroundPath());
				 czjawarduse.setAwardonId(czjawardon.getAwardonId());
				 int num = Integer.valueOf(czjawardon.getExt3());
				 num++;
			     Czjawardon czjawardon2 = new Czjawardon();
			     czjawardon2.setAwardonId(czjawardon.getAwardonId());
			     czjawardon2.setExt3(num);
			     czjawardonService.updateCzjawardon(czjawardon2);
			 }
			 
			 
			 
			 
			 //奖品库分配表加一
			 Map<String, Object> param2 = new HashMap<String, Object>();
			 param2.put("awardstockId", award.getId());
             Czjawardstock czjawardstock  = czjawardstockService.getCzjawardstock(param2);              
             if(czjawardstock!=null){   
            	 
            	 InsertawardUsr(czjawarduse,czjawardstock.getAwardType());
            	 
            	 //判断是否有效奖品
        		 if(czjawardstock.getAwardType()!=6){
        			 arardMap.put("ext2","1");
        		 }else{
        			 arardMap.put("ext2","0");
        		 }	
            	 	            	 
            	 if(czjawardstock.getZhongjiangnum()==null){
            		 czjawardstock.setZhongjiangnum(0);
            	 }      	 
            	 int zhongjiangnum = czjawardstock.getZhongjiangnum() +1;
            	 czjawardstock.setZhongjiangnum(zhongjiangnum);
            	 czjawardstockService.updateCzjawardstock(czjawardstock);
             }                         		 
			 jsonResult(1,"","线上抽取奖品信息成功",arardMap,response);
			 }else{
			  jsonResult(0,"111","轮盘不存在或没添加线上奖品或奖品已经抽完了或者没有分配奖品到轮盘","",response);	 
			 }
								
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0,"208","线上抽取奖品信息错误,系统异常","",response);
			log.error("获取线上抽取奖品信息错误,系统异常",e);
			
		}		
	}	
		
	
    /***
     * 
     * @author 
     * @date 2017年5月27日 下午2:11:56
     * @param request
     * @param response
     * @param model
     */
	@RequestMapping("awardcode.do")
	public synchronized void  awardcode( HttpServletRequest request,
			@RequestParam(required=true) String awardonId,
			@RequestParam(required=true) String userInfoId,
			HttpServletResponse response, ModelMap model) {
		try {
			
			//查询用户存在才能抽奖
			Map<String, Object> paramUserId = new HashMap<String, Object>();
			paramUserId.put("id", userInfoId);
			Czjuserinfo czjuserinfo = czjuserinfoService.getCzjuserinfo(paramUserId);
			if(czjuserinfo == null){
				jsonResult(0,"305","不存在的用户不给与抽奖","",response);
				return;
			}							
			 Map<String, Object> czjawardonParam = new HashMap<String, Object>();
			 czjawardonParam.put("awardonId", awardonId);
			 Czjawardon czjawardon123 = czjawardonService.getCzjawardon(czjawardonParam);
			
			 String  rouletteId ="";
			 if(czjawardon123 !=null){				 
				   rouletteId = czjawardon123.getShopId();
			 }else{
			   	jsonResult(0,"304","二维码活动没设置奖品","",response);
			   	return; 
			 }			 
				//查询活动是否开始与结束
				Map<String, Object> roulettepParam = new HashMap<String, Object>();
				roulettepParam.put("rouletteId", rouletteId);
				Czjroulette czjroulette = czjrouletteService.getCzjroulette(roulettepParam);
				Date date = new Date();
				if(czjroulette == null){
					jsonResult(0,"301","不存在的转盘","",response);
					return;
				}else{
				   	if(date.getTime()<czjroulette.getStarttime().getTime()){
				   		jsonResult(0,"302","活动还没开始","",response);
				   		return;
				   	}
					
				   	if(date.getTime()>czjroulette.getEndtime().getTime()){
				   		jsonResult(0,"303","活动已经结束","",response);
				   		return;
				   	}
				}			 
			 
			//限制每个人只能抽一次轮盘
			Czjawarduse czjawarduse1 = queryAwar(rouletteId,userInfoId);
			if(czjawarduse1 !=null){
				Map<String, Object> map1 = new HashMap<String, Object>();
				  map1.put("ext1", czjawarduse1.getExt1());
				  map1.put("ext2", czjawarduse1.getExt2());
				  map1.put("name",czjawarduse1.getAwardId());
				  map1.put("awardId",czjawarduse1.getAwarduseId());
				  map1.put("userState",1);
			      map1.put("received",11);
			        Map<String, Object> map = new HashMap<String, Object>();
			        map.put("status", 1);
			        map.put("code", "");
			        map.put("msg", "二维码奖品只能抽一次奖");
			        map.put("result", map1);
			        writeJson(response, JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue));				  
				 return;
			}
			//查出对应轮盘所有的奖品和奖品库限制的数量
			Map<String, Object> param = new HashMap<String, Object>();	
			param.put("rouletteId",rouletteId);
			List<CzjawardonVOB> list = czjawardapiService.getawardonListALLVOB(param);

			//奖品数量多少
			int listTrue = list.size();
			if(listTrue ==0){				
				jsonResult(0,"123","没有分配奖品信息","",response);	
				return;
			}
			//变化之前的多少数量
            int after = list.size();
			
			
			for(int i = list.size() -1 ; i >= 0; i--){
				CzjawardonVOB czjawardonVOB = list.get(i);
				
				if(czjawardonVOB.getFenpeicount()==null){
					czjawardonVOB.setFenpeicount(0);
				}
				if(czjawardonVOB.getChouqunum()==null){
					czjawardonVOB.setChouqunum(0);
				}				
                int fenpei =  czjawardonVOB.getFenpeicount();
                int chouqu =  czjawardonVOB.getChouqunum();
                if(chouqu >= fenpei || czjawardonVOB.getProbability()==0 ){                	     
                	list.remove(i);                												  	
			    }
			}
			//变化后的代码
			after = list.size();
			
			if(listTrue > after &&  after ==0){				
				jsonResult(0,"110","奖品已经抽取完了","",response);	
				return;
			}		
			List<Award> awards = new ArrayList<Award>();
			//放到抽奖工具类
			for(CzjawardonVOB czjawardonVOB : list){
				Award award = new Award();
				award.setId(czjawardonVOB.getAwardstockId());
				if(czjawardonVOB.getProbability()==null){
					czjawardonVOB.setProbability(0.1);
				}
				award.setProbability(czjawardonVOB.getProbability());
				award.setName(czjawardonVOB.getThingName());
				award.setExt1(czjawardonVOB.getExt1());
				award.setExt2(czjawardonVOB.getExt2());
				award.setExt3(czjawardonVOB.getShangpinId());
				award.setFestivalActId(czjawardonVOB.getFestivalActId());
				award.setBusinessId(czjawardonVOB.getBusinessId());
				award.setDiscountPrice(czjawardonVOB.getDiscountPrice());
				award.setPrice(czjawardonVOB.getPrice());
				award.setDiscountType(czjawardonVOB.getDiscountType());
				award.setAwardonId(czjawardonVOB.getAwardonId());
				award.setSmallprice(czjawardonVOB.getSmallprice());
				awards.add(award);
			}
			//开始抽奖
			 Map<String,Object> arardMap = new HashMap<String, Object>();
			 if(awards.size()>0){
			 Award award = Award.lottery(awards);	
			 arardMap.put("awardId",award.getId());
			 arardMap.put("name",award.getName());
			 arardMap.put("ext1",award.getExt1());
			 //插入奖品表
			 Czjawarduse czjawarduse = new Czjawarduse();
			 czjawarduse.setAwardId(award.getId());
			 czjawarduse.setUserInfoId(userInfoId);
			 czjawarduse.setRouletteId(rouletteId);
			 czjawarduse.setAwardType(1);
			 czjawarduse.setExt1(award.getExt1());
			 czjawarduse.setExt2(award.getExt2());
			 czjawarduse.setExt3(award.getExt3());
			 czjawarduse.setFestivalActId(award.getFestivalActId());
			 czjawarduse.setBusinessId(award.getBusinessId());
			 czjawarduse.setDiscountType(award.getDiscountType());
			 czjawarduse.setDiscountPrice(award.getDiscountPrice());
			 czjawarduse.setPrice(award.getPrice());
			 czjawarduse.setSmallprice(award.getSmallprice());
			 //奖品库信息插入来
			 
			 Map<String, Object> getCzjawardonParm = new HashMap<String, Object>();
			 getCzjawardonParm.put("awardonId", award.getAwardonId());
			 Czjawardon czjawardon = czjawardonService.getCzjawardon(getCzjawardonParm);
			 if(czjawardon!=null){
				 czjawarduse.setProbability(czjawardon.getProbability());
				 czjawarduse.setAwardcount(czjawardon.getCount());
				 czjawarduse.setAwardOrder(Integer.valueOf(czjawardon.getExt1()));
				 czjawarduse.setIstrue(1);
				 czjawarduse.setChouqunum(Integer.valueOf(czjawardon.getExt3()));
				 czjawarduse.setActPath(czjawardon.getPrizePath());
				 czjawarduse.setBackgroundPath(czjawardon.getBackgroundPath());
				 czjawarduse.setAwardonId(czjawardon.getAwardonId());
				 int num = Integer.valueOf(czjawardon.getExt3());
				 num++;
			     Czjawardon czjawardon2 = new Czjawardon();
			     czjawardon2.setAwardonId(czjawardon.getAwardonId());
			     czjawardon2.setExt3(num);
			     czjawardonService.updateCzjawardon(czjawardon2);
			 }
			 			 			 			 
			 //奖品库分配表加一
			 Map<String, Object> param2 = new HashMap<String, Object>();
			 param2.put("awardstockId", award.getId());
             Czjawardstock czjawardstock  = czjawardstockService.getCzjawardstock(param2);              
             if(czjawardstock!=null){
            	 
            	 InsertawardUsr(czjawarduse,czjawardstock.getAwardType());
            	 
            	 //判断是否有效奖品
        		 if(czjawardstock.getAwardType()!=6){
        			 arardMap.put("ext2","1");
        		 }else{
        			 arardMap.put("ext2","0");
        		 }	
        		          	
            	 if(czjawardstock.getZhongjiangnum()==null){
            		 czjawardstock.setZhongjiangnum(0);
            	 }      	 
            	 int zhongjiangnum = czjawardstock.getZhongjiangnum() +1;
            	 czjawardstock.setZhongjiangnum(zhongjiangnum);
            	 czjawardstockService.updateCzjawardstock(czjawardstock);
             }                         		 
			 jsonResult(1,"","二维码奖品信息成功",arardMap,response);
			 }else{
			  jsonResult(0,"111","二维码不存在或没添加二维码奖品或奖品已经抽完了或者没有分配奖品到二维码","",response);	 
			 }
								
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0,"208","二维码奖品信息错误,系统异常","",response);
			log.error("获取二维码奖品信息错误,系统异常",e);
			
		}		
	}				
        /**
         * 
         * @author 
         * @date 2017年6月10日 下午6:05:08
         * @param request
         * @param response
         * @param model
         */
		@RequestMapping("rouletteonList.do")
		public synchronized void  rouletteonList( HttpServletRequest request,
				@RequestParam(required=true)  String rouletteId1,
				HttpServletResponse response, ModelMap model) {
			try {
			   if("".equals(rouletteId1)){
				   jsonResult(0,"110","rouletteId1为空字符串，请输入正确的字符串","",response);
				   return;
			   }			
			  Map<String, Object> param = new HashMap<String, Object>();
			  param.put("rouletteId", rouletteId1);
	          List<Czjroulette> czjroulettes = czjrouletteService.getCzjrouletteListALL(param);
	          if(czjroulettes.size()<1){
	        	  jsonResult(0,"302","没找到对应的轮盘，无效的rouletteId1","",response);	
	          }
	          List<Map<String,Object>> listmap = new ArrayList<Map<String,Object>>();
	          for(Czjroulette czjroulette : czjroulettes){
	        	  String rouletteId = czjroulette.getRouletteId();
	        	  String name = czjroulette.getRemark();
	        	  Map<String, Object> param1 = new HashMap<String, Object>();
	        	  param1.put("shopId", rouletteId);
	        	  List<CzjawardonVO> czjawardons = czjawardapiService.getawardonListALLVO(param1);
	        	  Map<String, Object> map = new HashMap<String, Object>();
	        	  map.put("roulettePath", czjroulette.getImgPath());
	        	  map.put("pointer", czjroulette.getImgUrl());	        	  
	        	  map.put("rouletteId", rouletteId);
	        	  map.put("name", name);
	        	  map.put("orderNum", czjroulette.getImgOrder());
	        	  map.put("roulettecontent", czjroulette.getRoulettecontent());
	        	  map.put("czjawardons", czjawardons);
	        	  listmap.add(map);
	          }
	          	          				
		      jsonResult(1,"","获取线上轮盘信息成功",listmap,response);			 
				
			} catch (Exception e) {
				e.printStackTrace();
				jsonResult(0,"208","获取线上轮盘信息错误,系统异常","",response);
				log.error("获取线上轮盘信息错误,系统异常",e);
				
			}		
		} 
			
	/**
	 * 
	 * @author 
	 * @date 2017年6月12日 下午6:14:30
	 * @param rouletteId
	 * @param userInfoId
	 * @return
	 */
     public  Czjawarduse queryAwar(String rouletteId,String userInfoId ){
    	 Map<String, Object> param = new HashMap<String, Object>();
    	 param.put("rouletteId",rouletteId);
    	 param.put("userInfoId",userInfoId);
    	 Czjawarduse czjawarduse = czjawarduseService.getCzjawarduseExistence(param);
    	 return czjawarduse;
     }
     
     /**
      * 
      * @author 
      * @date 2017年6月12日 下午6:14:23
      * @param czjawarduse
      */
     public void InsertawardUsr(Czjawarduse czjawarduse,Integer type){
    	 czjawarduse.setCount(1);
		 czjawarduse.setCreatetime(new Date());
		 
		 Integer integer =0;
		 if(type !=4 ){
			 integer =  OrderStatus.UNUSE.getIndex();
		 }else{
			 integer = OrderStatus.UNRECEIVED.getIndex();
		 }
		 //1是有效  0是无效
		 String ext2 ="";
		 //0是显示 1是不显示
		 Integer isHidden = 0;
		 if(type !=6){
			 ext2 = "1";
			 isHidden = 0;
		 }else{
			 ext2 = "0";
			 isHidden = 1;
		 }		 
		 czjawarduse.setStatus(integer);
		 czjawarduse.setExt2(ext2);
		 czjawarduse.setIsHidden(isHidden);
		 czjawarduse.setOrderNo(OrderNumUtils.getOrderNo(4));
    	 czjawarduseService.insertCzjawarduse(czjawarduse);
     }
		

     
        /**
         * 
         * @author 
         * @date 2017年6月13日 下午4:05:23
         * @param request
         * @param userInfoId
         * @param pageNo
         * @param order
         * @param sort
         * @param response
         * @param model
         */
		@RequestMapping("awardUserList.do")
		public  void  awardUserList( HttpServletRequest request,
				@RequestParam(required=true) String userInfoId,
				Integer pageNo, String order,Integer pageSize,
				String sort,
				HttpServletResponse response, ModelMap model) {
			try {
		   		if (pageNo == null)
		   			pageNo = 1;
		   		if (pageSize == null)
		   			pageSize = 10;
		   		Map<String, Object> param = new HashMap<String, Object>();
		   		param.put("userInfoId",userInfoId);
				Pagination pagination = czjawarduseService.getCzjawarduseListAll(param, pageSize, pageNo, order, sort);	   
				
			    List<CzjawarduseVO> czjawarduseVOs = (List<CzjawarduseVO>) pagination.getList();
			    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    Date now = new Date();
		        for(CzjawarduseVO czjawarduseVO : czjawarduseVOs){
		        	 if(czjawarduseVO.getAwardStockType() !=null && czjawarduseVO.getAwardStockType()==4){
		        		 czjawarduseVO.setHongbaoId(czjawarduseVO.getAwarduseId());
		        	 }
		        	 if(czjawarduseVO.getAwardStockType() !=null && czjawarduseVO.getAwardStockType()==3){
		        		 czjawarduseVO.setIscode(1);
		        	 }else{
		        		 czjawarduseVO.setIscode(0);
		        	 }
		        	 Date awardDate = sdf.parse(czjawarduseVO.getValidDate());
		        	 if(now.getTime()>awardDate.getTime()){
		        		 czjawarduseVO.setIsEffective(1);
		        	 }else{
		        		 czjawarduseVO.setIsEffective(0);
		        	 }
		        	 
		        }
		        pagination.setList(czjawarduseVOs);						
		      jsonResult(1,"","获取用户奖品信息成功",pagination,response);			 				
			} catch (Exception e) {
				e.printStackTrace();
				jsonResult(0,"208","获取用户奖品息错误,系统异常","",response);
				log.error("获取用户奖品信息错误,系统异常",e);
				
			}		
		}  
     
        /**
         * 
         * @author 
         * @date 2017年6月13日 下午4:05:23
         * @param request
         * @param userInfoId
         * @param pageNo
         * @param order
         * @param sort
         * @param response
         * @param model
         */
		@RequestMapping("awardReceive.do")
		public  void  awardUserList( HttpServletRequest request,
				@RequestParam(required=true) String awarduseId,
				@RequestParam(required=true) String serviceName,
				@RequestParam(required=true) String countTel,
				@RequestParam(required=true) String countName,
				String countNum,
				HttpServletResponse response, ModelMap model) {
			try {
				if("undefined".equals(serviceName) || "".equals(serviceName) ){
					jsonResult(0,"213","你的服务区名称无效","",response);
				}
				if("undefined".equals(countTel) || "".equals(countTel) ){
					jsonResult(0,"214","你的电话号码无效","",response);
				}
				if("undefined".equals(countName) || "".equals(countName) ){
					jsonResult(0,"215","你的姓名无效","",response);
				}
				if(countNum ==null || "undefined".equals(countNum) || "".equals(countNum) ){
					countNum ="1";
				}					
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("awarduseId", awarduseId);
				Czjawarduse czjawarduse = czjawarduseService.getCzjawarduse(param);
				if(czjawarduse!=null){
					//使用
					if(czjawarduse.getStatus()==OrderStatus.USE.getIndex()){
						jsonResult(0,"210","你的奖品已经使用过了","",response);
						return;
					}else{
						if(czjawarduse.getStatus()==OrderStatus.UNUSE.getIndex()){											
						czjawarduse.setStatus(OrderStatus.USE.getIndex());
						czjawarduse.setServiceName(serviceName);
						czjawarduse.setCountTel(countTel);
						czjawarduse.setCountDate(new Date());
						czjawarduse.setCountName(countName);
						czjawarduse.setCountNum(countNum);
						czjawarduseService.updateCzjawarduse(czjawarduse);
						 jsonResult(1,"","使用奖品成功","",response);	
						}	 
					}
					//领取
					if(czjawarduse.getStatus()==OrderStatus.RECEIVED.getIndex()){
						jsonResult(0,"212","你的奖品已经领取过了","",response);
						return;
					}else{
						if(czjawarduse.getStatus()==OrderStatus.UNRECEIVED.getIndex()){	
						czjawarduse.setStatus(OrderStatus.RECEIVED.getIndex());
						czjawarduse.setServiceName(serviceName);
						czjawarduse.setCountTel(countTel);
						czjawarduse.setCountDate(new Date());
						czjawarduse.setCountName(countName);
						czjawarduse.setCountNum(countNum);						
						czjawarduseService.updateCzjawarduse(czjawarduse);
						 jsonResult(1,"","领取奖品成功","",response);	
					  }	 
					}					
				}else{
					jsonResult(0,"211","奖品不存在或奖品用户Id是无效值","",response);
				}																		     		 				
			} catch (Exception e) {
				e.printStackTrace();
				jsonResult(0,"208","领取奖品信息错误,系统异常","",response);
				log.error("领取奖品信息错误,系统异常",e);
				
			}		
		}  
		
        /**
         * 
         * @author 
         * @date 2017年6月13日 下午4:05:23
         * @param request
         * @param userInfoId
         * @param pageNo
         * @param order
         * @param sort
         * @param response
         * @param model
         */
		@RequestMapping("awarddelete.do")
		public  void  awarddelete( HttpServletRequest request,
				@RequestParam(required=true) String awarduseId,
				HttpServletResponse response, ModelMap model) {
			try {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("awarduseId", awarduseId);
				Czjawarduse czjawarduse = czjawarduseService.getCzjawarduse(param);
				if(czjawarduse!=null){					
						czjawarduse.setIsHidden(1);;
						czjawarduseService.updateCzjawarduse(czjawarduse);
						jsonResult(1,"","删除奖品成功","",response);
				}else{
					jsonResult(0,"211","奖品不存在或奖品用户Id是无效值","",response);
				}																		     		 				
			} catch (Exception e) {
				e.printStackTrace();
				jsonResult(0,"208","删除奖品信息错误,系统异常","",response);
				log.error("删除奖品信息错误,系统异常",e);
				
			}		
		}  		
		
        /**
         * 
         * @author 
         * @date 2017年6月13日 下午4:05:23
         * @param request
         * @param userInfoId
         * @param pageNo
         * @param order
         * @param sort
         * @param response
         * @param model
         */
		@RequestMapping("awardDetails.do")
		public  void  awardDetails( HttpServletRequest request,
				@RequestParam(required=true) String awarduseId,
				HttpServletResponse response, ModelMap model) {
			try {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("awarduseId", awarduseId);
				CzjawarduseALLMessage czjawarduse = czjawarduseService.getCzjawarduseALLMessage(param);

			 jsonResult(1,"","获取奖品详情成功",czjawarduse,response);	
		
		
																						     		 				
			} catch (Exception e) {
				e.printStackTrace();
				jsonResult(0,"208","获取奖品信息错误,系统异常","",response);
				log.error("获取奖品信息错误,系统异常",e);
				
			}		
		}  		
     
		
		
		
     
	@Resource
	private CzjawardapiService czjawardapiService;
	
	@Resource
	private CzjrouletteService czjrouletteService;	
	
	@Resource
	private CzjrouleoffService czjrouleoffService;	
	
	@Resource
	private CzjawarduseService czjawarduseService;
	
	@Resource
	private CzjawardstockService czjawardstockService;
	
	@Resource
	private CzjawardonService czjawardonService;
	
	@Resource
	private CzjuserinfoService czjuserinfoService;
	
}